package com.opengroupe.dto;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.opengroupe.database.qRHSQLiteHelper;



public class CategorieDto implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3296000521606872806L;
	private Integer idCategorie;
	private String libelleCategorie;
	
	private Integer idQuestionnaire;
	
	private QuestionnaireDto questionnaire;
	
	private List<QuestionDto> questions;
	
	public CategorieDto() {
	}

	public CategorieDto(int id, String libelleCategorie, int idQuestionnaire) {
		this.idCategorie = id;
		this.libelleCategorie = libelleCategorie;
		this.idQuestionnaire = idQuestionnaire;
	}
	
	public List<QuestionDto> getQuestions() {
		return this.questions;
	}
	
	public void setQuestions(List<QuestionDto> questions) {
		this.questions = questions;
	}

	public Integer getIdCategorie() {
		return this.idCategorie;
	}
	
	public void setIdCategorie(Integer idCategorie) {
		this.idCategorie = idCategorie;
	}
	
	public QuestionnaireDto getQuestionnaire() {
		return this.questionnaire;
	}
	
	public void setQuestionnaire(QuestionnaireDto questionnaire) {
		this.questionnaire = questionnaire;
	}
	
	public Integer getIdQuestionnaire() {
		return this.idQuestionnaire;
	}

	public void setIdQuestionnaire(Integer idQuestionnaire) {
		this.idQuestionnaire = idQuestionnaire;
	}
	
	

	
	

	public String getLibelleCategorie() {
		return this.libelleCategorie;
	}

	public void setLibelleCategorie(String libelleCategorie) {
		this.libelleCategorie = libelleCategorie;
	}


	public void save(Context context)
	{
		qRHSQLiteHelper bddHelper = new qRHSQLiteHelper(context);
		SQLiteDatabase bdd = bddHelper.getWritableDatabase();
		
		
		
		ContentValues values = new ContentValues();
		values.put("libelle", libelleCategorie);
		
		if (idCategorie == 0)
		{
			values.put("idQuestionnaire", idQuestionnaire);
			
			// insert
			long id= bdd.insert("categorie", null, values);
			idQuestionnaire = (int) id;
		}
		else
		{
			bdd.update("categorie", values, "id=?",new String[] { String.valueOf( this.idCategorie) });
			
		}
		
		
		bdd.close();
		bddHelper.close();
		
	}
	
	public void delete(Context context)
	{
		qRHSQLiteHelper bddHelper = new qRHSQLiteHelper(context);
		SQLiteDatabase bdd = bddHelper.getWritableDatabase();			
		
		bdd.delete("categorie", "id=?",new String[] { String.valueOf( this.idCategorie) });
		
		bdd.close();
		bddHelper.close();
		
	}
	
	public static CategorieDto getFromBdd(Context context, int idCategorie) {
		CategorieDto categorie = null;
		
		qRHSQLiteHelper bddHelper = new qRHSQLiteHelper(context);
		SQLiteDatabase bdd = bddHelper.getReadableDatabase();
	    
	    Cursor cursor = null;
	    
	    
	    
	    // Categorie
	    cursor = bdd.query("categorie",
	    		new String[] { "id","libelle", "idQuestionnaire" },
	    		"id=?", 
	    		new String[] {String.valueOf(idCategorie) },
	    		null,null,null,null);
	    
		

		cursor.moveToFirst();
		
		
	    if  (!cursor.isAfterLast()) {
	    	categorie = new CategorieDto(
	    			cursor.getInt(0),
	    			cursor.getString(1),
	    			cursor.getInt(2));
	    	
	    }
	    cursor.close();
	    
	    
	    
	    // Questionnaire
	    cursor = bdd.query("questionnaire",
	    		new String[] { "id","libelle" },
	    		"id=?", 
	    		new String[] {String.valueOf(categorie.getIdQuestionnaire()) },
	    		null,null,null,null);
	    
	    cursor.moveToFirst();
	    
	    if  (!cursor.isAfterLast()) {
	    	categorie.setQuestionnaire( new QuestionnaireDto(
	    			cursor.getInt(0),
	    			cursor.getString(1)));
	    	
	    }
	    cursor.close();
    
	    // Questions
		
		cursor = bdd.query("question",
				new String[]  { "id",	"contenu", "idCategorie" },
				"idCategorie=?", 
				new String[] { String.valueOf(idCategorie) },
				null,null,null,null);
		
		cursor.moveToFirst();
		
		QuestionDto question = null;
		ArrayList<QuestionDto> questions = new ArrayList<QuestionDto>();
		
		while  (!cursor.isAfterLast()) {
			question = new QuestionDto( cursor.getInt(0),cursor.getString(1), cursor.getInt(2) );
			questions.add( question);

			cursor.moveToNext();
	    }
	    cursor.close();
	    
	    categorie.setQuestions(questions);
		
	    bdd.close();
	    bddHelper.close();
	    
		return categorie;
	}
}

